﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Web;
using MaxLeaf.Common;

namespace MaxLeaf.CallWebService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                string strType = args[0];
                string strWSName = args[1];
                string strDBName = args[2];
                LogManager.Debug( "Load" + strWSName + " " + strDBName + ":" + DateTime.Now.Date.ToShortTimeString());  
                //System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(
                //    delegate { CallWindowWebServices(strType, strWSName, strDBName); }));
                //t.Start();
                CallWindowWebServices(strType, strWSName, strDBName);
            }
            catch (Exception ex)
            {

                LogManager.Error(ex);

                if (!File.Exists(@"C:\maxLeaf\WebK3\A.txt"))
                {
                    File.CreateText(@"C:\maxLeaf\WebK3\A.txt");
                }
                File.WriteAllText(@"C:\maxLeaf\WebK3\A.txt", ex.ToString());
            }
        }

        public static void OnCallComplete(IAsyncResult ar)
{
    LogManager.Debug("Finished " + DateTime.Now.Date.ToShortTimeString());  

}

        private static void CallWindowWebServices(string strType, string WSName, string strDBName)
        {
            try
            {
                string strServerName = "K3Master";
                ServiceReference1.MaxLeafWebServiceSoapClient tads = new ServiceReference1.MaxLeafWebServiceSoapClient(WSName);
                //AsyncCallback cb = new AsyncCallback(OnCallComplete);

                //tads.BeginCheckK3Log(strDBName, cb, null);
                LogManager.Debug("Begin " + WSName + " " + strDBName + ":" + DateTime.Now.Date.ToShortTimeString());  
                tads.CheckK3Log(strDBName);
                LogManager.Debug("finish " + WSName + " " + strDBName + ":" + DateTime.Now.Date.ToShortTimeString());  
            }
            catch (Exception ex)
            {
                LogManager.Error(ex);
                if (!File.Exists(@"C:\maxLeaf\WebK3\" + WSName + "_" + strDBName + ".txt"))
                {
                    File.CreateText(@"C:\maxLeaf\WebK3\" + WSName + "_" + strDBName + ".txt");
                }
                File.WriteAllText(@"C:\maxLeaf\WebK3\" + WSName + "_" + strDBName + ".txt", ex.ToString());
            }
        }
    }
}
